﻿using HermesPlugin.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.GPS;
using ZoneFiveSoftware.Common.Data;
using System.Windows.Forms;

namespace HermesPlugin.Export
{
    class ExportTCX
    {
        #region Private Methods
        static string GetTCXHeader()
        {
            return Resources.String_File_TCX_Header;
        }

        static string GetTCXFooter()
        {
            return Resources.String_File_TCX_Footer;
        }

        static string GetTCXDate(DateTime time)
        {
            string dateTime = string.Empty;
            dateTime = string.Format("{0}-{1}-{2}", time.Year.ToString(), time.Month.ToString().PadLeft(2, '0'), time.Day.ToString().PadLeft(2, '0'));
            return dateTime;
        }

        static string GetTCXTime(DateTime time)
        {
            string dateTime = string.Empty;
            dateTime = time.TimeOfDay.ToString();
            return dateTime;
        }

        static string GetTrackPoints(IActivity activity)
        {
            double totalDistance = .0;
            string trackPoints = string.Empty;

            for (int i = 0; i < activity.GPSRoute.Count; i++ )
            {
                ITimeValueEntry<float> itemHeartRate = activity.HeartRatePerMinuteTrack[i];
                ITimeValueEntry<IGPSPoint> itemGPSPoint = activity.GPSRoute[i];
                if ( i > 0 )
                    totalDistance += itemGPSPoint.Value.DistanceMetersToPoint(activity.GPSRoute[i-1].Value);
                trackPoints += string.Format(   Resources.String_File_TCX_TrackPoint,
                                                GetTCXDate(activity.StartTime.AddSeconds(itemGPSPoint.ElapsedSeconds)),
                                                GetTCXTime(activity.StartTime.AddSeconds(itemGPSPoint.ElapsedSeconds)),
                                                itemGPSPoint.Value.LatitudeDegrees.ToString(),
                                                itemGPSPoint.Value.LongitudeDegrees.ToString(),
                                                itemGPSPoint.Value.ElevationMeters.ToString(),
                                                totalDistance.ToString(),
                                                itemHeartRate.Value.ToString());
                trackPoints += System.Environment.NewLine;
            }

            return trackPoints;
        }

        static string GetLaps(IActivity activity)
        {
            double averageSpeed = ( activity.TotalDistanceMetersEntered / activity.TotalTimeEntered.TotalSeconds ) * 3.6;
            return string.Format(   Resources.String_File_TCX_Lap,
                                    GetTCXDate(activity.StartTime),
                                    GetTCXTime(activity.StartTime),
                                    activity.TotalTimeEntered.TotalSeconds.ToString(),
                                    activity.TotalDistanceMetersEntered.ToString(),
                                    averageSpeed.ToString(),
                                    activity.TotalCalories.ToString(),
                                    activity.AverageHeartRatePerMinuteEntered.ToString(),
                                    activity.MaximumHeartRatePerMinuteEntered.ToString(),
                                    GetTrackPoints(activity));
        }
        #endregion

        public static void ExportActivity(IActivity activity, string file)
        {
            StreamWriter streamOut = new StreamWriter(file, false);
            streamOut.WriteLine(GetTCXHeader());
            // real content
            streamOut.WriteLine(string.Format(  Resources.String_File_TCX_Activity,
                                                GetTCXDate(activity.StartTime),
                                                GetTCXTime(activity.StartTime),
                                                GetLaps(activity)) );
            // end of content
            streamOut.WriteLine(GetTCXFooter());
            streamOut.Close();
        }
    }
}
